package com.returnsoft.softphone.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;

import com.returnsoft.softphone.dto.TransferDto;
import com.returnsoft.softphone.eao.CampaignEao;
import com.returnsoft.softphone.eao.TransferEao;
import com.returnsoft.softphone.entity.Campaign;
import com.returnsoft.softphone.entity.Transfer;
import com.returnsoft.softphone.exception.ConversionExcepcion;
import com.returnsoft.softphone.exception.CCenterExcepcion;
import com.returnsoft.softphone.exception.EaoExcepcion;
import com.returnsoft.softphone.util.Conversion;

/**
 * Session Bean implementation class TransferBean
 */
@Stateless
@LocalBean
public class TransferBean implements TransferInterface , Serializable{
	private static final long serialVersionUID = 1L;
	@EJB
	private TransferEao transferEao;
	@EJB
	private CampaignEao campaignEao;
	@EJB
	private Conversion conv;

	/**
	 * Default constructor.
	 */
	public TransferBean() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public List<TransferDto> findTransfer(Integer agentId)
			throws CCenterExcepcion {

		try {
			List<Campaign> campaigns = campaignEao.findByAgent(agentId);
			List<TransferDto> transfersDto = new ArrayList<TransferDto>();
			for (Campaign campaign : campaigns) {
				List<Transfer> transfers = transferEao.findTransfers(campaign
						.getId());
				for (Transfer transfer : transfers) {
					TransferDto transferDto = conv.fromTransfer(transfer);
					Boolean exist = Boolean.FALSE;
					for (int i = 0; i < transfersDto.size(); i++) {
						TransferDto transferDtoV = transfersDto.get(i);
						if (transferDtoV.getId().equals(transferDto.getId())) {
							exist = Boolean.TRUE;
						}
					}
					if (!exist) {
						transfersDto.add(transferDto);
					}
				}
			}
			return transfersDto;
		} catch (EaoExcepcion e) {
			throw new CCenterExcepcion(e.getMessage());
		} catch (ConversionExcepcion e) {
			throw new CCenterExcepcion(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new CCenterExcepcion(e.getMessage());
		}
	}

}
